package main;

import Socket.DataToPC;
import Socket.SocketSingleton;
import handler.Receiver;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Scanner;
import javax.swing.*;
import model.Arena;
import view.Drawer;
import model.Robot;

public class Main extends JApplet {
    
    private static Graphics2D g;
        
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        Drawer.getInstance().setGraphic(g2);
        Drawer.getInstance().setArena(Arena.getInstance());
        Drawer.getInstance().setRobot(Robot.getInstance());
        Drawer.getInstance().draw();
        
        Receiver r = new Receiver();
        while (true) {
            Scanner sc = new Scanner(System.in);
            r.receive(sc.nextLine());
        }        
    }    

    public static void main(String s[]) {
        try{	
            System.out.println("Running main thread");

            SocketSingleton sockets = SocketSingleton.getInstance();
            // create sender thread from arduino to algo
            DataToPC arduinoData = new DataToPC(sockets.getArduinoSocket());

            //create sender thread from nexus to algo
            DataToPC n7Data = new DataToPC(sockets.getN7Socket());
            arduinoData.start();
            n7Data.start();
            JFrame f = new JFrame("Arena | Visualization");
            f.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
            });
            JApplet applet = new Main();
            f.getContentPane().add("Center", applet);
            applet.init();
            f.pack();
            f.setSize(new Dimension(620,500));
            f.setVisible(true);
            arduinoData.join();
            n7Data.join();
            sockets.getArduinoSocket().close();
            sockets.getN7Socket().close();
            System.out.println("Main terminating...");
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }


}